package com.lb.jwitter.action;

import java.util.Map;

import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.action.util.SchedulerManager;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblDKMHLogMailAction {
	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	private TblTaiKhoan taiKhoanDangNhap; 
	 public TblTaiKhoan getTaiKhoanDangNhap() { 
	     return taiKhoanDangNhap; 
	 } 
	 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	     this.taiKhoanDangNhap = taiKhoanDangNhap; 
	 } 
	 private TblQuyenChiTietService tblQuyenChiTietService;
	 
	private SchedulerManager schedulerManager;

	public String startScheduler() {
		try {
			Map session = ActionContext.getContext().getSession();
		     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
		     if( taiKhoanDangNhap == null) return "noprivilege";
		     
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			schedulerManager = (SchedulerManager) context
					.getBean("schedulerManager");
			schedulerManager.startScheduler();
			result = "OK";
			return Action.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
			return Action.ERROR;
		}
	}

	public String stopScheduler() {
		try {
			Map session = ActionContext.getContext().getSession();
		     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
		     if( taiKhoanDangNhap == null) return "noprivilege";
		     ApplicationContext context = ApplicationContextProvider
				.getApplicationContext();
		     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
		     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_LogMail", "xem")){ 
		         return "noprivilege"; 
		     }
		     
			
			schedulerManager = (SchedulerManager) context
					.getBean("schedulerManager");
			schedulerManager.stopScheduler();
			result = "OK";
			return Action.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
			return Action.ERROR;
		}
	}
	
	public String form() {
		try {
			Map session = ActionContext.getContext().getSession();
		     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
		     if( taiKhoanDangNhap == null) return "noprivilege";
		     ApplicationContext context = ApplicationContextProvider
				.getApplicationContext();
		     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
		     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_LogMail", "xem")){ 
		         return "noprivilege"; 
		     }
		     schedulerManager = (SchedulerManager) context
				.getBean("schedulerManager");
		     isRunning = schedulerManager.isRunning();
		     return Action.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
			return Action.ERROR;
		}
	}
	private Boolean isRunning;

	public Boolean getIsRunning() {
		return isRunning;
	}

	public void setIsRunning(Boolean isRunning) {
		this.isRunning = isRunning;
	}
}
